package com.my.loginbmob.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.my.loginbmob.R;
import com.my.loginbmob.constant.MyConstant;
import com.my.loginbmob.dialog.LoadingDialog;
import com.my.loginbmob.presenter.ILoginPresenter;
import com.my.loginbmob.presenter.LoginPresenter;
import com.my.loginbmob.view.iview.ILoginView;

import cn.bmob.v3.Bmob;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, ILoginView {
    private EditText etUserName, etPassword;
    private Button btnLogin;
    private TextView tvRegister;
    private ILoginPresenter iLoginPresenter;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initListener();
    }

    private void initView() {
        etUserName = findViewById(R.id.et_user_name);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        tvRegister = findViewById(R.id.tv_register);
        loadingDialog = new LoadingDialog(this);

        // new 接口
        iLoginPresenter = new LoginPresenter(this);
        // 初始化Bmob
        Bmob.initialize(this, MyConstant.MY_CONSTANT_BMOB_APPLICATION_ID);
    }

    private void initListener() {
        btnLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: // 登录
                setProgressVisibility(1);
                iLoginPresenter.goLogin(etUserName.getText().toString(), etPassword.getText().toString());
                break;
            case R.id.tv_register: // 注册
                goIntent(RegistActivity.class);
                break;
        }
    }

    /**
     * 页面跳转
     *
     * @param cla
     */
    private void goIntent(Class cla) {
        Intent intent = new Intent(LoginActivity.this, cla);
        startActivity(intent);
    }

    /**
     * 页面跳转
     *
     * @param cla
     */
    private void goIntent(Class cla, String msg) {
        Intent intent = new Intent(LoginActivity.this, cla);
        intent.putExtra("login", msg);
        startActivity(intent);
    }

    /**
     * 错误吐司提示
     *
     * @param msg
     */
    @Override
    public void setErroToast(String msg) {
        toastUtil(msg);
    }

    /**
     * 吐司工具类
     *
     * @param msg
     */
    private void toastUtil(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 加载页面
     *
     * @param visibility
     */
    @Override
    public void setProgressVisibility(int visibility) {
        if (visibility == 1) {
            loadingDialog.show();
        } else {
            loadingDialog.dismiss();
        }
    }

    /**
     * 登录结果返回
     *
     * @param result
     * @param msg
     */
    @Override
    public void loginResule(boolean result, String msg) {
        if (result) { // 登录成功
            goIntent(MainActivity.class, etUserName.getText().toString());
            finish();
        } else {
            toastUtil(msg);
        }
    }

}
